<?php

namespace App\Http\Controllers\Property;

use App\Common\json;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FellController extends Controller{






    public function sendoop($id){
        $data        = DB::table('user')
                     ->select('user_id','user_start','user_end')
                     ->join('friends','user.user_id','=','friends.user_start')
                     ->where('user_start','=',$id)
                     ->where('oper',2)
                     ->get();
        return $data;

    }

    public function sendopen($id){
        $value      = $this ->sendoop($id);

        foreach ($value as $key) {
            $id   = $key ->user_end;
            $data = DB::table('user')
                ->select('user_id')
                ->where('user_id', '!=', $id)
                ->get();
        }
            return $data;
      /*  $data       = DB::table('user')
                    ->where('')*/

    }

    public function endoop($id){
        $data        = DB::table('friends')
            ->select('user_id','oper','user_start','user_end')
            ->leftjoin('user','friends.user_end','=','user.user_id')
            ->where('user_end','=',$id)
            ->where('oper','=',2)
            ->get();
        return $data;
    }


    public function user_start($id){
        $data_start = DB::table('friends')
            ->select('user_id', 'phone', 'head', 'nickname', 'sex', 'sign', 'identity',
                'user_address', 'user_village', 'floor', 'number', 'room', 'oper', 'blacklist','user_roomname')
            ->leftjoin('user', 'user.user_id', '=', 'friends.user_end')
            ->where('user_start', $id)
            ->where('oper','=',2)
            ->where('blacklist',0)
            ->get();
        return $data_start;
    }

    public function user_end($id){
        $data_start = DB::table('friends')
            ->select('user_id', 'phone', 'head', 'nickname', 'sex', 'sign', 'identity',
                'user_address', 'user_village', 'floor', 'number', 'room', 'oper', 'blacklist','user_roomname')
            ->leftjoin('user', 'user.user_id', '=', 'friends.user_start')
            ->where('user_end', $id)
            ->where('oper','=',2)
            ->where('blacklist',0)
            ->get();
        return $data_start;
    }


    public function getfriends(Request $request){
        if ($request->isMethod('post')) {
            $user_start = $request->input('user_start');
            $select = DB::table('friends')
                ->where('user_start', $user_start)
                ->orwhere('user_end', $user_start)
                ->where('oper','=',2)
                ->get();
            if ($select == true) {
                $userstart = $this->user_start($user_start);
                $userend = $this->user_end($user_start);
                $dd = array_merge($userstart, $userend);
                foreach ($dd as $key) {
                    $head = $key->head;
                    $nickname = $key->nickname;
                    $sex = $key->sex;
                    $sign = $key->sign;
                    $identity = $key->identity;
                    $user_address = $key->user_address;
                    $user_village = $key->user_village;
                    $user_roomname = $key ->user_roomname;
/*                    $floor = $key->floor;
                    $number = $key->number;
                    $room = $key->room;*/
                    if (empty($key->oper)) {
                        $oper = 0;
                    } else {
                        $oper = $key->oper;
                    }
                    if (empty($key->blacklist)) {
                        $blacklist = 0;
                    } else {
                        $blacklist = $key->blacklist;
                    }
                    $da[] = array(
                        'user_id' => $key->user_id,
                        'phone' => $key->phone,
                        'head' => $head,
                        'nickname' => $nickname,
                        'sex' => $sex,
                        'sign' => $sign,
                        'identity' => $identity,
                        'address' => $user_address,
                        'village_id' => $user_village,
                        'user_roomname' =>$user_roomname,
/*                        'floor' => $floor,
                        'number' => $number,
                        'room' => $room,*/
                        'oper' => $oper,
                        'blacklist' => $blacklist,
                    );
                }
                if(empty($da)){
                    $pd  = array();
                }else{
                    $pd  = $da;
                }
                return json::json('success', '获取关联好友', $pd);
            } else {
                return json::json('success', '获取关联好友', []);
            }
        }

    }


  /*  public function ppo(Request $request,$type){
        if($request ->isMethod('post')){

        }
    }*/



    public function ppos(Request $request)
    {

    }




}